var gulp=require("gulp");
var gulp_min_css=require("gulp-minify-css");
var gulp_sass=require("gulp-sass");
var gulp_plumber=require("gulp-plumber");
var gulp_tinypng=require("gulp-tinypng");
var gulp_uglify=require("gulp-uglify");
var browser_sync = require("browser-sync").create();
var gulp_autoprefixer=require("gulp-autoprefixer");

//scss转css
gulp.task("scss",function(){
	gulp.src('src/scss/**/*.scss')
		.pipe(gulp_plumber())
		.pipe(gulp_sass())
		.pipe(gulp_autoprefixer({
			browsers: ['last 2 versions', 'Android >= 4.0','iOS 7','last 3 Safari versions'],
			cascade: false
		}))
		.pipe(gulp.dest('src/css/'));
})
//css压缩
gulp.task("css",function(){
	gulp.src("src/scss/**/*.scss")
		.pipe(gulp_plumber())
		.pipe(gulp_sass())
		.pipe(gulp_min_css(
			{
				keepSpecialCommentes:'*'
			}
		))
		.pipe(gulp_autoprefixer({
			browsers: ['last 2 versions', 'Android >= 4.0','iOS 7','last 3 Safari versions'],
			cascade: false
		}))
		.pipe(gulp.dest("dist/css"));
	gulp.src("src/css/**/*.css")
		.pipe(gulp_min_css(
			{
				keepSpecialCommentes:'*'
			}
		))
		.pipe(gulp.dest("dist/css/"));
});
//图片压缩
gulp.task("img",function(){
	gulp.src(["src/img/*","!src/img/*.{ico,gif}"])
		.pipe(gulp_plumber())
		.pipe(gulp_tinypng("tCk7pDoZL2Qd5LQ00bozb1w_LrMTfoQu"))
		.pipe(gulp.dest("dist/img/"));
});
//js压缩
gulp.task("uglify",function(){
	gulp.src("src/js/**/*.js")
		.pipe(gulp_uglify())
		.pipe(gulp.dest('dist/js/'));
});


//定义自动刷新浏览器任务

gulp.task("browser", function() {
    browser_sync.init({
    	port:3000,
        server: {
            baseDir: "./src/"
        }
    });
    gulp.watch("src/scss/*",["scss"]).on("change",browser_sync.reload);
    gulp.watch("src/*.html").on("change",browser_sync.reload);
});

//复制文件
gulp.task("copy",function(){
	gulp.src("src/img/*.{ico,gif}").pipe(gulp.dest("dist/img/"));
	gulp.src("src/font/*").pipe(gulp.dest("dist/font/"}));
	gulp.src("src/*.html").pipe(gulp.dest("dist/"));
});

//定义一键开发模式
gulp.task('dev',['browser']);

//定义一键生成模式
gulp.task("pro",["copy","uglify","img","css"]);
